package sz.szsmk.citizencard.face;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.ui.activity.FaceAuthActivity;

public class BottomButtonFragment extends FaceFragment{

    public static final String EXTRA_STATE = "extra_state";

    private TextView mButtonView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bottom_button, container, false);
        mButtonView = (TextView)view.findViewById(R.id.btn_start);
        mButtonView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Build.VERSION.SDK_INT >= 23){
                    List<String> permissions = null;
                    if (getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                        PackageManager.PERMISSION_GRANTED) {
                        permissions = new ArrayList<>();
                        permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                    }
                    if (getActivity().checkSelfPermission(Manifest.permission.CAMERA) !=
                        PackageManager.PERMISSION_GRANTED) {
                        if (permissions == null) {
                            permissions = new ArrayList<>();
                        }
                        permissions.add(Manifest.permission.CAMERA);
                    }
                    if (permissions == null) {
                        startLivenessActivity();
                    } else {
                        String[] permissionArray = new String[permissions.size()];
                        permissions.toArray(permissionArray);
                        // Request the permission. The result will be received
                        // in onRequestPermissionResult()
                        requestPermissions(permissionArray, 0);
                    }
                } else {
                    startLivenessActivity();
                }
            }
        });
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        int state = FaceAuthActivity.STATE_START;
        if (getArguments() != null) {
            state = getArguments().getInt(EXTRA_STATE, FaceAuthActivity.STATE_START);
        }
        mButtonView.setText(state == FaceAuthActivity.STATE_START ? R.string.txt_start_detect : R.string.txt_detect_again);
        mButtonView.setEnabled(true);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 0) {
            // Request for WRITE_EXTERNAL_STORAGE permission.
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startLivenessActivity();
            } else {
                // Permission request was denied.
                Toast.makeText(getActivity(), R.string.txt_error_permission, Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void startLivenessActivity() {
        mButtonView.setEnabled(false);
    }
}